/*
 * @copyright 2011 Gerald Jacobson
 * @license GNU General Public License
 * 
 * This file is part of GameTimer.
 *
 * GameTimer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * GameTimer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GameTimer. If not, see <http://www.gnu.org/licenses/>.
 */
package org.amphiption.gametimer.screen.component;

import org.amphiption.gametimer.entity.Player;
import org.amphiption.gametimer.util.StringUtil;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;

import com.amphiprion.prototype.ScreenProperty;
import com.amphiprion.prototype.component.AreaComponent;
import com.amphiprion.prototype.util.BitmapUtil;
import com.amphiprion.prototype.util.NormalizedBitmap;

public class PlayerComponent extends AreaComponent {
	private Player player;
	private Paint paint;
	private Paint paintText;
	private Paint paintTime;
	private boolean docked;
	private boolean turnMode;
	private NormalizedBitmap panel;
	private String uri;

	public PlayerComponent(Player player, float x, float y, boolean turnMode) {
		super(player.getColor(), x, y, 150, 74, 255);
		uri = "panel.png";
		this.turnMode = turnMode;
		this.player = player;

		paint = new Paint();

		paintText = new Paint();
		paintText.setColor(Color.BLACK);
		paintText.setTextSize(20.0f);
		paintText.setAntiAlias(true);

		paintTime = new Paint();
		paintTime.setColor(-player.getColor());
		paintTime.setTextSize(20.0f);
		paintTime.setAntiAlias(true);

	}

	@Override
	public void onDraw(Canvas canvas, ScreenProperty screenProperty) {
		super.onDraw(canvas, screenProperty);
		drawImage(canvas, panel.bitmap, x, y, scale, screenProperty);

		drawText(canvas, player.getName(), x, y - height / 4, scale, screenProperty, paintText);
		if (turnMode) {
			drawText(canvas, "" + player.getCounter(), x, y + height / 4 - 3, scale, screenProperty, paintTime);
		} else {
			drawText(canvas, StringUtil.durationToString(player.getCounter()), x, y + height / 4 - 3, scale, screenProperty, paintTime);
		}
	}

	protected void drawImage(Canvas canvas, Bitmap img, float centerX, float centerY, float scale, ScreenProperty screenProperty) {
		if (alpha == 0 || img == null) {
			return;
		}
		// paint.setAlpha(alpha);
		Matrix m = new Matrix();
		m.postTranslate(-img.getWidth() / 2, -img.getHeight() / 2);
		m.postRotate(screenProperty.screenRotation + rotation);
		m.postScale(scale * screenProperty.screenScale, scale * screenProperty.screenScale);

		if (screenProperty.screenRotation == 0) {
			m.postTranslate(centerX * screenProperty.screenScale + screenProperty.screenOffsetX, centerY * screenProperty.screenScale + screenProperty.screenOffsetY);
		} else {
			m.postTranslate(canvas.getWidth() - centerY * screenProperty.screenScale + screenProperty.screenOffsetX, centerX * screenProperty.screenScale
					+ screenProperty.screenOffsetY);
		}
		canvas.drawBitmap(img, m, paint);
	}

	private void drawText(Canvas canvas, String str, float centerX, float centerY, float scale, ScreenProperty screenProperty, Paint painter) {

		painter.setAlpha(alpha);
		float pSize = paintText.getTextSize();
		float txtX = centerX;
		float txtY = centerY;
		// if ("center".equals(alignment)) {
		txtX -= scale * paintText.measureText(str) / 2;
		// }
		txtX = txtX * screenProperty.screenScale;
		txtY = txtY * screenProperty.screenScale;
		painter.setTextSize(pSize * screenProperty.screenScale * scale);
		Matrix m = new Matrix();
		// m.postTranslate(-canvas.getWidth() / 2, -canvas.getHeight() / 2);
		m.postRotate(screenProperty.screenRotation);
		if (screenProperty.screenRotation == 0) {
			// m.postTranslate(canvas.getWidth() / 2, canvas.getHeight() / 2);
		} else {
			m.postTranslate(canvas.getWidth(), 0);
			// m.postTranslate(canvas.getHeight() / 2, canvas.getWidth() / 2);
		}
		txtY += pSize * 0.3 * screenProperty.screenScale * scale;

		canvas.save();
		canvas.setMatrix(m);
		canvas.rotate(rotation, x * screenProperty.screenScale, y * screenProperty.screenScale);
		canvas.drawText(str, txtX - screenProperty.screenOffsetX, txtY - screenProperty.screenOffsetY, painter);
		// canvas.drawRect(0, 0, 400, 240, paint);
		canvas.restore();
		painter.setTextSize(pSize);

	}

	public boolean isDocked() {
		return docked;
	}

	public void setDocked(boolean docked) {
		this.docked = docked;
	}

	public Player getPlayer() {
		return player;
	}

	@Override
	public void activate() {
		panel = BitmapUtil.loadBitmap(uri);
		super.activate();
	}

	@Override
	public void desactivate() {
		super.desactivate();
		BitmapUtil.unloadBitmap(uri);
	}
}
